//
//  HNsnpClipImage.swift
//  swift微博项目
//
//  Created by huodian on 16/10/6.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

extension UIImage{
    
    class  func snpClipImage()->UIImage{
        
        let window = UIApplication.shared.keyWindow
        
        UIGraphicsBeginImageContextWithOptions(window!.bounds.size, false, UIScreen.main.scale)
        
        window?.drawHierarchy(in: window!.bounds, afterScreenUpdates: false)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        
        UIGraphicsEndImageContext()
        
        return image!
        
        
    }
    
    
    func scaleImage(width:CGFloat)->UIImage{
        
        let imageW = self.size.width
        
        let imageH  = self.size.height
        
        if width > imageW{
            
            return self
        }
        
        let scaleHeight = width * imageH / imageW
        
        let bounds = CGRect(x: 0, y: 0, width: width, height: scaleHeight)
        
        UIGraphicsBeginImageContext(bounds.size)
        
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
        
        draw(in: bounds)
        
        let image  = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        
        return image!
        
        
        
        
        
        
        
    }
    
    
}
